package com.scalar.chiptrack.sales.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.sales.forms.quote.QuotePopupFieldsForm;
import com.scalar.chiptrack.sales.forms.ShippingMethodPopupForm;
import com.scalar.chiptrack.sales.dao.quote.QuotationDAO;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.QuoteType;
import com.scalar.chiptrack.sales.QuotePopupField;
import com.scalar.chiptrack.sales.QuoteApplicationType;
import com.scalar.chiptrack.sales.ShippingMethod;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Labels;
import com.scalar.chiptrack.users.User;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.MessageResources;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Sujan
 * Date: July 8, 2008
 * Time: 12:49:14 PM
 */
public class ShippingMethodPopupAction extends ChipTrackAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);
        if (user == null)
        {
            request.setAttribute ( "isSessionExpired", "true" );
            return mapping.findForward ("success");
        }
        ShippingMethodPopupForm shippingMethodPopupForm = (ShippingMethodPopupForm)form;
        String action = request.getParameter ( "action" );
        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
        if ( "load".equalsIgnoreCase( action ) )
        {
            ArrayList custShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getCustomerId() );
            ArrayList endCustShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getEndCustomerId() );
            shippingMethodPopupForm.setCustShippingMethodsList( custShippingMethodList );
            shippingMethodPopupForm.setEndCustShippingMethodsList( endCustShippingMethodList );
        }
        else if ( "save".equalsIgnoreCase( action ) )
        {
            try
            {
                if ( shippingMethodPopupForm.getShippingMethodId() <= 0 )
                {
                    if ( CustomerDAO.doesShippingMethodExist ( null, shippingMethodPopupForm.getCustomerId(), shippingMethodPopupForm.getShippingMethod(), shippingMethodPopupForm.getShippingAcc() ) )
                    {
                        errors.put( MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" ) + " already exists", null );
                    }
                    else
                    {
                        CustomerDAO.insertShippingMethod ( null, shippingMethodPopupForm.getCustomerId(), shippingMethodPopupForm.getShippingMethod(), shippingMethodPopupForm.getShippingAcc() );
                        info.put ( MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" ) + " saved successfully", null );
                    }
                }
                else
                {
                    
                    if ( CustomerDAO.doesShippingMethodExist ( null, shippingMethodPopupForm.getShippingMethodCustId(), shippingMethodPopupForm.getShippingMethod(), shippingMethodPopupForm.getShippingAcc() ) )
                    {
                        errors.put( MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" ) + " already exists", null );
                    }
                    else
                    {
                        NewCustomerDAO.updateShippingMethod ( null, shippingMethodPopupForm.getShippingMethodId(), shippingMethodPopupForm.getShippingMethod(), shippingMethodPopupForm.getShippingAcc() );
                        info.put ( MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" ) + " saved successfully", null );
                    }
                }
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not save Shipping Method", null );
                e.printStackTrace();
            }
            ArrayList custShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getCustomerId() );
            ArrayList endCustShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getEndCustomerId() );
            shippingMethodPopupForm.setCustShippingMethodsList( custShippingMethodList );
            shippingMethodPopupForm.setEndCustShippingMethodsList( endCustShippingMethodList );
            shippingMethodPopupForm.setShippingMethod( null );
            shippingMethodPopupForm.setShippingAcc( null );
            shippingMethodPopupForm.setShippingMethodId(-1);
            shippingMethodPopupForm.setShippingMethodCustId(-1);

            request.setAttribute( "refreshListData", "true");
        }
        else if ( "edit".equalsIgnoreCase( action ) )
        {
            ArrayList custShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getCustomerId() );
            ArrayList endCustShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getEndCustomerId() );
            shippingMethodPopupForm.setCustShippingMethodsList( custShippingMethodList );
            shippingMethodPopupForm.setEndCustShippingMethodsList( endCustShippingMethodList );
            ShippingMethod shippingMethod = NewCustomerDAO.getShippingMethod( null, shippingMethodPopupForm.getShippingMethodId() );
            if ( shippingMethod != null )
            {
                shippingMethodPopupForm.setShippingMethod( shippingMethod.getShippingmethod() );
                shippingMethodPopupForm.setShippingAcc( shippingMethod.getShippingacc() );
                shippingMethodPopupForm.setShippingMethodCustId( shippingMethod.getCustomerId() );
            }
        }
        else if ( "disable".equalsIgnoreCase( action ) )
        {
            try
            {
                NewCustomerDAO.updateShippingMethodStatus( null, shippingMethodPopupForm.getShippingMethodId(), true );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not disable Shipping Method", null );
                e.printStackTrace();
            }
            ArrayList custShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getCustomerId() );
            ArrayList endCustShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getEndCustomerId() );
            shippingMethodPopupForm.setCustShippingMethodsList( custShippingMethodList );
            shippingMethodPopupForm.setEndCustShippingMethodsList( endCustShippingMethodList );
            shippingMethodPopupForm.setShippingMethod( null );
            shippingMethodPopupForm.setShippingAcc( null );
            shippingMethodPopupForm.setShippingMethodId(-1);
            shippingMethodPopupForm.setShippingMethodCustId(-1);
            request.setAttribute( "refreshListData", "true");
        }
        else if ( "activate".equalsIgnoreCase( action ) )
        {
            try
            {
                 NewCustomerDAO.updateShippingMethodStatus( null, shippingMethodPopupForm.getShippingMethodId(), false );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not activate Shipping Method", null );
                e.printStackTrace();
            }
            ArrayList custShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getCustomerId() );
            ArrayList endCustShippingMethodList = NewCustomerDAO.getCustomerShippingMethodList( null, shippingMethodPopupForm.getEndCustomerId() );
            shippingMethodPopupForm.setCustShippingMethodsList( custShippingMethodList );
            shippingMethodPopupForm.setEndCustShippingMethodsList( endCustShippingMethodList );
            shippingMethodPopupForm.setShippingMethod( null );
            shippingMethodPopupForm.setShippingAcc( null );
            shippingMethodPopupForm.setShippingMethodId(-1);
            shippingMethodPopupForm.setShippingMethodCustId(-1);
            request.setAttribute( "refreshListData", "true");
        }

        return mapping.findForward ("success");
    }
}
